﻿using DevExpress.XtraEditors.Controls;
using DevTeam;
using System;
using System.Data;
using System.Drawing;
using System.IO;

namespace ConfExpert.Forms.Du4
{
    public partial class TopDuzeltme : XPopupForm
    {
        int RollId = 0;
        public TopDuzeltme(int _RollId)
        {
            InitializeComponent();
            RollId = _RollId;
        }

        private void Du4Duzeltme_Load(object sender, EventArgs e)
        {
            
            btnGuncelle.Enabled = false;
            gleProduct.Properties.Assign(RI(Rp.V_Kumas));
            gleOwner.Properties.Assign(RI(Rp.V_Customer));
            gleTela.Properties.Assign(RI(Rp.Tela));

            txtQty.EditValueChanged += new EventHandler(txtQty_EditValueChanged);
            gleOwner.EditValueChanged += new EventHandler(gleOwner_EditValueChanged);
            gleProduct.EditValueChanged += new EventHandler(gleProduct_EditValueChanged);
            gleTela.EditValueChanged += gleTela_EditValueChanged;

            DataTable dt = ExecuteDataTable("GetRoll0", RollId);
            if (dt.Rows.Count > 0)
            {
                btnGuncelle.Enabled = true;
                DataRow dr = dt.Rows[0];
                txtInfo.Caption = dr["Product"].ToString();
                this.Text += (" - " + RollId.ToString());

                if (dr["Photo"] != DBNull.Value)
                {
                    picture.Visible = true;
                    MemoryStream ms = new MemoryStream((byte[])dr["Photo"]);
                    Image pimg = Image.FromStream(ms);
                    picture.Image = pimg;
                    //img.Size = pimg.Size;
                    picture.Properties.SizeMode = PictureSizeMode.Stretch;
                }
            }
            else
            {
                btnGuncelle.Enabled = false;
            }
        }

        void Clr(string o)
        {
            ord = o;

            if (o != "P")
                gleProduct.EditValue = null;

            if (o != "O")
                gleOwner.EditValue = null;

            if (o != "Q")
                txtQty.EditValue = null;

            if (o != "T")
                gleTela.EditValue = null;
        }

        bool sup = false;
        string ord = string.Empty;
        void gleProduct_EditValueChanged(object sender, EventArgs e)
        {
            if (!sup)
            {
                sup = true;
                Clr("P");
                sup = false;
            }
        }

        void gleOwner_EditValueChanged(object sender, EventArgs e)
        {
            if (!sup)
            {
                sup = true;
                Clr("O");
                sup = false;
            }
        }

        void txtQty_EditValueChanged(object sender, EventArgs e)
        {
            if (!sup)
            {
                sup = true;
                Clr("Q");
                sup = false;
            }
        }

        void gleTela_EditValueChanged(object sender, EventArgs e)
        {
            if (!sup)
            {
                sup = true;
                Clr("T");
                sup = false;
            }
        }

        private void btnGüncelle_Click(object sender, EventArgs e)
        {
            int ownerId = 0;
            int productId = 0;
            int telaId = 0;
            decimal qty = -1;

            if (txtNotes.Text.Trim().Length > 0)
            {
                if (txtQty.Text.Trim().Length > 0)
                    decimal.TryParse(txtQty.Text, out qty);

                ownerId = intParse(gleOwner.EditValue);
                productId = intParse(gleProduct.EditValue);
                telaId = intParse(gleTela.EditValue);
                if (qty > -1 || productId > 0 || telaId>0 || ownerId > 0)
                {
                    if (intParse(Session.DataManager.ExecuteScalar("InsDu4Duzeltme", RollId, txtNotes.Text, qty, ownerId, productId, telaId, WhoAmI)) > 0)
                        ShowInfoMessage("Top güncellendi.");
                    txtNotes.Text = "";
                    //picture.Visible = false;
                    //lblProc.Text = "";

                    sup = true;
                    Clr("CLEAR ALL");
                    sup = false;
                }
                else
                    ShowErrorMessage("Miktar, ProductId, TelaId ve OwnerId'den biri dolu olmalı.");
            }
            else
                ShowErrorMessage("Açıklama zorunlu alandır.");
        }
    }
}
